//
//  oldDateTools.m
//  laonianbangSupport
//
//  Created by 勇拓 李 on 17/11/11.
//  Copyright © 2017年 zaiyou. All rights reserved.
//

#import "oldDateTools.h"

@implementation oldDateTools

+ (NSString *)getDateFromDateToNow:(NSDate *)date
{
    // 3.字符串转换成时间/自动转换0时区/东加西减
    NSDate *now = [NSDate date];
    
    // 注意获取calendar,应该根据系统版本判断
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSCalendarUnit type = NSCalendarUnitYear |
    NSCalendarUnitMonth |
    NSCalendarUnitDay |
    NSCalendarUnitHour |
    NSCalendarUnitMinute |
    NSCalendarUnitSecond;
    
    // 4.获取了时间元素
    NSDateComponents *cmps = [calendar components:type fromDate:date toDate:now options:0];
    
    NSString *str_hour = [NSString stringWithFormat:@"%02ld",cmps.hour];//时
    NSString *str_minute = [NSString stringWithFormat:@"%02ld",cmps.minute];//分
    NSString *str_second = [NSString stringWithFormat:@"%02ld", cmps.second];//秒
    NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];
    
    return format_time;
    
}

+ (NSDate *) NSstringToNSdate:(NSString *)time
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:sss"];
    
    NSDate *resDate = [formatter dateFromString:time];
    
    return resDate;
}

+ (NSString *) compareCurrentTime:(NSString *)str ifNeedSuffix:(NSString *)suffix
{
    
    //把字符串转为NSdate
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *timeDate = [dateFormatter dateFromString:str];
    
    //得到与当前时间差
    NSTimeInterval  timeInterval = [timeDate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    //标准时间和北京时间差8个小时
    //    timeInterval = timeInterval - 8*60*60;
    long temp = 0;
    NSString *result;
    if (timeInterval < 60) {
        if (suffix) {
            result = [NSString stringWithFormat:@"刚刚"];
        }
        else
        {
            result = [NSString stringWithFormat:@"小于一分钟"];
        }
    }
    else if((temp = timeInterval/60) <60){
        if (suffix) {
            result = [NSString stringWithFormat:@"%ld分钟%@",temp, suffix];
        }
        else
        {
            result = [NSString stringWithFormat:@"%ld分钟",temp];
        }
    }
    else if((temp = temp/60) <24){
        if (suffix) {
            result = [NSString stringWithFormat:@"%ld小时%@",temp, suffix];
        }
        else
        {
            result = [NSString stringWithFormat:@"%ld小时",temp];
        }
    }
    else{
        result = [[str componentsSeparatedByString:@" "] firstObject];
    }
    
    return  result;
}

@end
